package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Movie;
import com.bussiness.MovieBussinessProxy;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;
import com.exceptions.PendingIssuedMoviesException;

/**
 * Servlet implementation class DeleteMovieServlet
 */
public class DeleteMovieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	MovieBussinessProxy movieBI = new MovieBussinessProxy(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteMovieServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		RequestDispatcher dispatcher;
		Movie[] allmovieList;
		try
		{
			
			int movieid = Integer.parseInt(request.getParameter("movieid"));
			movieBI.setEndpoint(ServiceEndPoints.MovieEndPoint);
			movieBI.deleteMovie(movieid);
			//session.setAttribute("editMovie", movie);
			
			allmovieList = movieBI.getAllMovies();
			allmovieList=(Movie[])session.getAttribute("allMovies");
			request.setAttribute("ServerMessage","Deleted Successfully");
			dispatcher = getServletContext().getRequestDispatcher("/listdeletemovie.jsp");
			dispatcher.forward(request, response);
			
		}
		catch(PendingIssuedMoviesException pi){
			request.setAttribute("ServerMessage","Movie cannot be deleted.Its been Issued to Customers");
			dispatcher = getServletContext().getRequestDispatcher("/listdeletemovie.jsp");
			dispatcher.forward(request, response);
		}
		catch(Exception e)
		{
			request.setAttribute("ServerMessage", UIMessages.SERVICE_UNAVAILABLE);
			dispatcher = getServletContext().getRequestDispatcher("/listdeletemovie.jsp");
			dispatcher.forward(request, response);
		}
	}

}

